package test.list;

public class BinarySearchTree {
    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    //测试
    public static void test(){
        BinarySearchTree tree = new BinarySearchTree();
        //建树
        tree.insert(5);
        tree.insert(3);
        tree.insert(7);
        tree.insert(1);
        tree.insert(2);
        tree.insert(9);

        //查询
        System.out.println("查询");
        System.out.println(tree.search(5));
        System.out.println(tree.search(9));

        //删除
        System.out.println("删除");
        System.out.println(tree.remove(5));
        System.out.println(tree.remove(10));

    }
    public static void main(String[] args){
        test();
    }

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        TreeNode parent = null;
        if(root == null){
            root = node;
            return true;
        }
        for(TreeNode cur = root;cur != null;){
            if(key >= cur.key){
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if(key >= parent.key){
            parent.right = node;
        }else{
            parent.left = node;
        }
        return true;
    }

    //查找key是否存在
    public TreeNode search(int key) {
        if(root == null){
            return null;
        }
        for(TreeNode cur = root;cur != null;){
            if(key == cur.key){
                return cur;
            }else if(key > cur.key){
                cur = cur.right;
            }else{
                cur = cur.left;
            }
        }
        return null;
    }

    //删除key的值
    public boolean remove(int key) {
        TreeNode parent = root;
        for(TreeNode cur = root;cur != null;){
            if(key == cur.key){
                if(cur.left == null && cur.right == null){
                    //如果被删除节点没有左右子树
                    if(cur == parent.left){
                        parent.left = null;
                    }else{
                        parent.right = null;
                    }
                }else if(cur.left == null && cur.right != null){
                    //如果被删除节点没有左子树
                    if(cur == parent.left){
                        parent.left = cur.right;
                    }else{
                        parent.right = cur.right;
                    }
                }else if(cur.left != null && cur.right == null){
                    //如果被删除节点没有右子树
                    if(cur == parent.left){
                        parent.left = cur.left;
                    }else{
                        parent.right = cur.left;
                    }
                }else{
                    //被删除节点有左右子树
                    TreeNode cur2 = cur;
                    //找到被删除节点右子树中最小的数
                    while(cur2.left != null){
                        cur2 = cur2.left;
                    }
                    //用 RightMin 存储 cur2 的值
                    TreeNode RightMin = new TreeNode(cur2.key);
                    //用 cur2.right 替代 RightMin 的位置
                    cur2 = cur2.right;
                    //替代被删除节点
                    RightMin.left = cur.left;
                    RightMin.right = cur.right;
                    if(cur == parent.left){
                        parent.left = RightMin;
                    }else{
                        parent.right = RightMin;
                    }
                }
                return true;
            }else if(key > cur.key){
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        return false;
    }
}
